//
//  VisitorLoginView.swift
//  微博222
//
//  Created by jessica on 15/11/8.
//  Copyright © 2015年 jessica. All rights reserved.
//

//import Foundation
//修改为
import UIKit

//写一个代理方法
protocol VisitorLoginViewDelegate :NSObjectProtocol{

//登陆
    func visitorWillLogin()
    //注册
func visitorWillRegister()

}

class VisitorLoginView : UIView {
    
    //声明代理
    
    //默认是强引用 ,需要添加weak
    weak var visitorDelegate :VisitorLoginViewDelegate?
    
    
    @objc func loginDidBtnClick(){
    
        //实现代理方法
    visitorDelegate?.visitorWillLogin()
    }
    
    @objc func registerBtnDidClick(){
        
    visitorDelegate?.visitorWillRegister()
        
    }
    
    func setUIInfo(imageName : String?,title : String){

    iconView.hidden = false
        tipLabel.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
//            bringSubviewToFront(circleView)
        iconView.image = UIImage(named:imageName!)
        
        
        }else{
        
            startAnimation()
        
        }
    }
    
    private func startAnimation(){
    
    //transform.roration不可以随便写哦`
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT//无限次
        anim.toValue = 2 * M_PI //一圈
        anim.duration = 10//多少秒一圈
        anim.removedOnCompletion = false//转跳还是什么都不移除,除非关闭
        circleView.layer.addAnimation(anim, forKey: nil)

    }
    
//    func
    //重写构造方法
    init() {
        super.init(frame : CGRectZero)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
//如果重写 init(frame:) 系统会默认为这个控件是通过代码创建,这里可以不写
//        super.init(coder: aDecoder)
        setupUI()
    }
    
    
    
  private func setupUI(){
    
    
    addSubview(circleView)
    addSubview(iconView)
    addSubview(backView)
    
    addSubview(tipLabel)
    addSubview(registerBtn)
    addSubview(loginBtn)
        //设置frame布局失效
    
    for vi in subviews {
    
    vi.translatesAutoresizingMaskIntoConstraints = false
    
    }
    //设置布局
    //VFL
    //"view1.attr1 = view2.attr2 * multiplier + constant"
    // 注意: 布局的约束 尽量相对一个控件
    //圆圈的约束
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
    //大头标的约束
    addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
    
    //设置提示文案的约束
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
    //设置宽度约束
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
    addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
    
    //设置登录按钮的约束
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
    
    //设置注册按钮的约束
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
    
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
    
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
    
    //设置背景视图的约束
    //位移枚举 ||   []
    //对视图宽高约束的数值 [String : NSNumber]
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
    
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
    
    //设置背景颜色
    backgroundColor = UIColor(white: 0.93, alpha: 1)
    
    
    
    //添加点击事件
    loginBtn.addTarget(self, action: "loginDidBtnClick", forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    

    
    //先懒加载所有控件,旋转的圆
    private lazy var circleView :UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //中间的图标
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //第一张图有一部分遮挡的是一张图来的哦
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
//
//    //下面的文字
    private lazy var tipLabel : UILabel = {
    
    let Label = UILabel()
     Label.text = "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜"
        Label.textAlignment = NSTextAlignment.Center
        Label.font = UIFont.systemFontOfSize(15)
        Label.textColor = UIColor.lightGrayColor()
        Label.numberOfLines = 0
   return Label
    
    }()

    
    //懒加载登陆
    
    private lazy  var loginBtn : UIButton = {
    
    let btn =  UIButton()
        
    btn.setTitle("登录", forState: .Normal)
btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
    return btn
    }()
    
    //懒加载注册
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
        }()
}